﻿// *********************************************************************
// [DCOM Productions]
// [Copyright (C) DCOM Productions All rights reserved.]
// *********************************************************************

namespace BitFlex.Windows.Forms {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.ComponentModel.Design;

    /// <summary>
    /// 
    /// </summary>
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(Panel))]
    //[Designer("BitFlex.Windows.Forms.Design.WizardPageDesigner, BitFlex.Windows.Forms.Design", typeof(IDesigner))]
    [Designer(typeof(BitFlex.Windows.Forms.Design.WizardPageDesigner))]
    public partial class WizardPage : UserControl {
        /// <summary>
        /// Initializes a new instance of the BitFlex.Windows.Forms.WizardPage class
        /// </summary>
        public WizardPage() {
            InitializeComponent();
        }

        /// <summary>
        /// Triggered when the WizardPage is navigated from, and the next WizardPage is displayed. Used to finalize or unload data.
        /// </summary>
        [Category("Action")]
        [Description("Triggered when the WizardPage is navigated from, and the next WizardPage is displayed. Used to finalize or unload data.")]
        public event System.EventHandler<EventArgs> NavigatingFrom;
        /// <summary>
        /// EventHandler Wireup for NavigatingFrom
        /// </summary>
        internal void OnNavigatingFrom(object sender, EventArgs e) {
            if (NavigatingFrom != null) {
                NavigatingFrom(sender, e);
            }
        }

        /// <summary>
        /// Triggered when the WizardPage is navigated to, and displayed. Used to prepare the page or load data.
        /// </summary>
        [Category("Action")]
        [Description("Triggered when the WizardPage is navigated to, and displayed. Used to prepare the page or load data.")]
        public event System.EventHandler<EventArgs> NavigatedTo;
        /// <summary>
        /// EventHandler Wireup for NavigatedTo
        /// </summary>
        internal void OnNavigatedTo(object sender, EventArgs e) {
            if (NavigatedTo != null) {
                NavigatedTo(sender, e);
            }
        }

        /// <summary>
        /// Overrides the base.Dock property and hides it from DesignTime
        /// </summary>
        [Browsable(false)]
        public override DockStyle Dock {
            get {
                return base.Dock;
            }
            set {
                base.Dock = value;
            }
        }

        /// <summary>
        /// Gets or sets a System.Boolean value indicating whether the page must be valid before the user can navigate to the next panel
        /// </summary>
        [Category("Behavior")]
        [DefaultValue(false)]
        [Description("Gets or sets a System.Boolean value indicating whether the page must be valid before the user can navigate to the next panel")]
        public bool EnableValidation {
            get;
            set;
        }

        /// <summary>
        /// Gets the index in the Wizard that this page lies
        /// </summary>
        public int Index {
            get {
                Wizard parent = Parent as Wizard;
                return parent == null ? -1 : parent.Pages.IndexOf(this);
            }
        }

        private bool m_IsValid = false;
        /// <summary>
        /// Gets or sets a System.Boolean value indicating whether the control passes developer-implemented validation
        /// </summary>
        [Category("Behavior")]
        [DefaultValue(false)]
        [Description("Gets or sets a System.Boolean value indicating whether the control passes developer-implemented validation")]
        public bool IsValid {
            get {
                return m_IsValid;
            }
            set {
                m_IsValid = value;
                if (EnableValidation) {
                    Wizard wizard = Parent as Wizard;
                    if (wizard != null) {
                        if (m_IsValid) {
                            wizard.SetNextState(enabled: true);
                        }
                        else {
                            wizard.SetNextState();
                        }
                    }
                }
            }
        }

        private string m_Subtitle = string.Empty;
        /// <summary>
        /// Gets or sets the Subtitle of the wizard page
        /// </summary>
        [Category("Appearance")]
        [DefaultValue("")]
        [Description("Gets or sets the Subtitle of the wizard page")]
        public string Subtitle {
            get {
                return m_Subtitle;
            }
            set {
                m_Subtitle = value;
                Invalidate();
            }
        }

        private string m_Title = string.Empty;
        /// <summary>
        /// Gets or sets the Title of the wizard page
        /// </summary>
        [Category("Appearance")]
        [DefaultValue("")]
        [Description("Gets or sets the Title of the wizard page")]
        public string Title {
            get {
                return m_Title;
            }
            set {
                m_Title = value;
                Invalidate();
            }
        }

        protected override void OnControlAdded(ControlEventArgs e) {
            base.OnControlAdded(e);
            WizardPage page = e.Control as WizardPage;
            if (page != null) {
                throw new System.ArgumentException("A WizardPage cannot contain another WizardPage control");
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e) {
            if (DesignMode) {
                using (SolidBrush brush = new SolidBrush(Parent.BackColor)) {
                    e.Graphics.FillRectangle(brush, ClientRectangle);
                    brush.Color = BackColor;
                    Rectangle rect = ClientRectangle;
                    rect.X += 2;
                    rect.Y += 2;
                    rect.Height -= 4;
                    rect.Width -= 4;
                    e.Graphics.FillRectangle(brush, rect);
                }
            }
            else {
                base.OnPaintBackground(e);
            }
            PaintTitle(e.Graphics);
            PaintSubtitle(e.Graphics);
            PaintWatermark(e.Graphics);
        }

        private void PaintSubtitle(Graphics gdi) {
            if (!String.IsNullOrEmpty(Subtitle)) {
                using (SolidBrush brush = new SolidBrush(WindowsFonts.SecondaryInstructions.FontColor)) {
                    int y = gdi.MeasureString(Title, WindowsFonts.MainInstructions.Font).ToSize().Height;
                    gdi.DrawString(Subtitle, WindowsFonts.SecondaryInstructions.Font, brush, Padding.Left + 2, y);
                }
            }
        }

        private void PaintTitle(Graphics gdi) {
            if (!String.IsNullOrEmpty(Title)) {
                using (SolidBrush brush = new SolidBrush(WindowsFonts.MainInstructions.FontColor)) {
                    gdi.DrawString(Title, WindowsFonts.MainInstructions.Font, brush, Padding.Left, Padding.Top);
                }
            }
        }

        private void PaintWatermark(Graphics gdi) {
            Wizard wizard = Parent as Wizard;
            if (wizard != null && wizard.Watermark != null) {
                gdi.DrawImage(wizard.Watermark, wizard.ClientRectangle.Width - wizard.Watermark.Width - 3, 3,
                    wizard.Watermark.Width, wizard.Watermark.Height);
            }
        }
    }
}
